<template>
  <div class="first">
    <div class="content-scroll">
      <div class="title">院校地区<span>（多选）</span></div>
      <div class="feature-box-feature">
        <div
          :class="{
            'province-item': true,
            active: item.checked,
          }"
          v-for="(item, index) in provinceList"
          :key="index"
          @click="chooseSchoolFeature(item, 'province')"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="title">院校类型<span>（多选）</span></div>
      <div class="feature-box-feature">
        <div
          :class="{
            'province-item': true,
            active: item.checked,
          }"
          v-for="(item, index) in schoolTypeList"
          :key="index"
          @click="chooseSchoolFeature(item, 'schoolType')"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="title">院校特色<span>（多选）</span></div>
      <div class="feature-box-feature">
        <div
          :class="{
            'province-item': true,
            active: item.checked,
          }"
          v-for="(item, index) in schoolFeatureList"
          :key="index"
          @click="chooseSchoolFeature(item, 'feature')"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="title">办学类型<span>（多选）</span></div>
      <div class="feature-box-feature">
        <div
          :class="{
            'province-item': true,
            active: item.checked,
          }"
          v-for="(item, index) in schoolNatureList"
          :key="index"
          @click="chooseSchoolFeature(item)"
        >
          {{ item.name }}
        </div>
      </div>
      <div class="title">办学层次<span>（多选）</span></div>
      <div class="feature-box-feature">
        <div
          :class="{
            'province-item': true,
            active: item.checked,
          }"
          v-for="(item, index) in schoolLevelList"
          :key="index"
          @click="chooseSchoolFeature(item)"
        >
          {{ item.name }}
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useVolunteerStore } from '@/store/volunteer';
import provinceData from '@/utils/provinceData';
import schoolFeatureData from '@/utils/schoolFeatureData';
import schoolNatureData from '@/utils/schoolNatureData';
import schoolLevelData from '@/utils/schoolLevelData';
import schoolTypeData from '@/utils/schoolTypeData';

const store = useVolunteerStore();

const provinceList = ref([]);
const schoolTypeList = ref([]);
const schoolFeatureList = ref([]);
const schoolNatureList = ref([]);
const schoolLevelList = ref([]);
const getData = (list) => {
  return list.map((item) => ({
    ...item,
    checked: item.name === '不限', // 默认选中"不限"选项
  }));
};
const getList = () => {
  provinceList.value = getData(provinceData);
  schoolTypeList.value = getData(schoolTypeData);
  schoolFeatureList.value = getData(schoolFeatureData);
  schoolNatureList.value = getData(schoolNatureData);
  schoolLevelList.value = getData(schoolLevelData);
};

getList();

const chooseSchoolFeature = (item, flag = '') => {
  item.checked = !item.checked;
  if (flag === 'province') {
    // 点击了不限
    if (!item.code) {
      provinceList.value.forEach((item) => {
        item.checked = false;
      });
      item.checked = true;
    } else {
      // 点击了其他类型,重置不限
      provinceList.value[0].checked = false;
    }
  }
  if (flag === 'feature') {
    // 点击了不限
    if (!item.code) {
      schoolFeatureList.value.forEach((item) => {
        item.checked = false;
      });
      item.checked = true;
    } else {
      // 点击了其他类型,重置不限
      schoolFeatureList.value[0].checked = false;
    }
  } else if (flag === 'schoolType') {
    // 点击了不限
    if (!item.code) {
      schoolTypeList.value.forEach((item) => {
        item.checked = false;
      });
      item.checked = true;
    } else {
      // 点击了其他类型,重置不限
      schoolTypeList.value[0].checked = false;
    }
  }
  getSelectData();
};

const clear = () => {
  schoolTypeList.value.forEach((item) => {
    item.checked = item.name === '不限'; // 默认选中"不限"选项
  });
  schoolFeatureList.value.forEach((item) => {
    item.checked = item.name === '不限'; // 默认选中"不限"选项
  });
  schoolNatureList.value.forEach((item) => {
    item.checked = false;
  });
  schoolLevelList.value.forEach((item) => {
    item.checked = false;
  });
  getSelectData();
};

const getSelectData = () => {
  let selectObj = {};
  const chosenProvinceList = provinceList.value
    .filter((item) => item.checked && item.name !== '不限')
    .map((item) => item.code);
  const chosenProvinceNameList = provinceList.value
    .filter((item) => item.checked && item.name !== '不限')
    .map((item) => item.name);
  const chosenSchoolTypeList = schoolTypeList.value
    .filter((item) => item.checked)
    .map((item) => item.code)
    .join(',');
  const chosenSchoolFeatureList = schoolFeatureList.value
    .filter((item) => item.checked)
    .map((item) => item.code)
    .join(',');
  const chosenSchoolNatureList = schoolNatureList.value
    .filter((item) => item.checked)
    .map((item) => item.code)
    .join(',');
  const chosenSchoolLevelList = schoolLevelList.value
    .filter((item) => item.checked)
    .map((item) => item.code)
    .join(',');
  selectObj = {
    regionIdList: chosenProvinceList.length > 0 ? chosenProvinceList : [],
    regionNameList:
      chosenProvinceNameList.length > 0 ? chosenProvinceNameList : [],
    collegeType: chosenSchoolTypeList,
    dualClassName: chosenSchoolFeatureList,
    natureName: chosenSchoolNatureList,
    levelName: chosenSchoolLevelList,
  };

  store.setFirstStepData(selectObj);
};
</script>

<style scoped lang="scss">
.first {
  height: 100%;
  padding: 0 0 0 28px;
  .content-scroll {
    height: 100%;
    padding-right: 32px;
    padding-bottom: 32px;
    overflow-y: auto;
    .title {
      padding: 46px 0 28px 0;
      display: flex;
      align-items: flex-end;
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 26px;
      color: #222222;
      line-height: 22px;
      text-align: left;
      font-style: normal;
      span {
        font-size: 20px;
        color: #999999;
      }
      &:first-child {
        padding-top: 32px;
      }
    }
    .feature-box-feature {
      display: flex;
      flex-wrap: wrap;
      gap: 14px;
      .province-item {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        font-family: 'PingFang-SC, PingFang-SC';
        font-size: 26px;
        padding: 8px 28px;
        color: #333333;
        background: #f8f8f8;
        border: #ffffff 1px solid;
        &.active {
          color: $primary-color;
          border: $primary-color 1px solid;
          background: #e5edff;
        }
      }
      &:last-child {
        margin-bottom: 132px;
      }
    }
  }
  .bottom-btn {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 30px;
    background: #ffffff;
    box-shadow: 0px -2px 4px 0px rgba(188, 188, 188, 0.5);
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 24px;
    z-index: 2;

    .left-btn,
    .right-btn {
      flex-shrink: 0;
      width: 48%;
      height: 72px;
      background: #f8f8f8;
      border-radius: 6px;
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 24px;
      color: #999999;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
    }

    .left-btn {
      background: #f8f8f8;
      border: 1px solid #f8f8f8;
      &.active {
        border: 1px solid $primary-color;
        color: $primary-color;
      }
    }
    .right-btn {
      color: #ffffff;
      background: $primary-color;
    }
  }
}
</style>
